<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 垃圾分类
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        type="danger"
                        icon="el-icon-circle-plus"
                        class="handle-del mr10"
                        @click="addRubbish"
                >新增</el-button>
                <el-button
                        type="info"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="delAllSelection"
                >批量删除</el-button>
                <el-input v-model="query.name" prefix-icon="el-icon-search" clearable placeholder="请输入关键词" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <div class="handle-box">
                <el-radio-group v-model="query.sortId" @change="sortChange">
                    <el-radio-button label="">全部</el-radio-button>
                    <el-radio-button label="1">可回收物</el-radio-button>
                    <el-radio-button label="2">有害垃圾</el-radio-button>
                    <el-radio-button label="3">湿垃圾</el-radio-button>
                    <el-radio-button label="4">干垃圾</el-radio-button>
                </el-radio-group>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="index" label="序号" width="70" align="center">
                    <template slot-scope="scope">
                        {{scope.$index+1}}
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="名称" align="center"></el-table-column>
                <el-table-column prop="type" label="分类" align="center">
                    <template slot-scope="scope">
                        <el-tag effect="dark">{{scope.row.type==1?"可回收物":
                            scope.row.type==2?"有毒垃圾":
                            scope.row.type==3?"湿垃圾":
                            scope.row.type==4?"干垃圾":"未知"}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="status" label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag
                                :type="scope.row.status=='0'?'success':(scope.row.status==='1'?'danger':'')"
                        >{{scope.row.status=='0'?'正常':'删除'}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >{{scope.row.status==0?"删除":"恢复"}}</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="sizes, total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @size-change="handleSizeChange"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="名称">
                    <el-input v-model="form.name" placeholder="请输入内容"></el-input>
                </el-form-item>
                <el-form-item label="分类">
                    <el-select v-model="form.type" filterable placeholder="请选择">
                        <el-option
                                v-for="item in sortList"
                                :key="item.sortId"
                                :label="item.sortName"
                                :value="item.sortId">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelEdit">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            sortList:[
                {
                    sortId: 1,
                    sortName: "可回收物"
                },
                {
                    sortId: 2,
                    sortName: "有害垃圾"
                },
                {
                    sortId: 3,
                    sortName: "湿垃圾"
                },
                {
                    sortId: 4,
                    sortName: "干垃圾"
                }
            ],
            query: {
                sortId:'',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            form_bak: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.$axios.get(this.GLOBAL.httpUrl + "/rubbish/select", {
                params:this.query
            }).then(res=>{
                if(res.data.status == 200){
                    this.tableData = res.data.data.resultList;
                    this.pageTotal = res.data.data.totalCount;
                }
            })
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    let url = "/rubbish/delete"
                    if(row.status != 0){
                        url = "/rubbish/recover"
                    }
                    this.$axios.get(this.GLOBAL.httpUrl + url,{
                        params:{
                            id: row.id
                        }
                    }).then(res=>{
                        this.$message.success(res.data.message);
                        this.getData();
                    }).catch(error=>{
                        this.$message.warning('啊哦~~发生异常咯');
                    })
                })
                .catch(() => {
                    this.$message.success("取消删除");
                });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    let params = {
                        delList: JSON.stringify(this.delList)
                    }
                    let url = "/rubbish/deleteByList"
                    this.$axios.post(this.GLOBAL.httpUrl + url,params).then(res=>{
                        this.$message.success(res.data.message);
                        for (let i = 0; i < length; i++) {
                            str += this.multipleSelection[i].name + ' ';
                        }
                        this.$message.error(`删除了${str}`);
                        this.multipleSelection = [];
                        this.getData();
                    }).catch(error=>{
                        this.$message.warning('啊哦~~发生异常咯');
                    })
                })
                .catch(() => {
                    this.$message.success("取消删除");
                });


        },
        //新增
        addRubbish(){
            this.form = {
                type: 1,
                name: null
            };
            this.editVisible = true;
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            if(this.form.type == null || this.form.type == ""){
                this.$message.warning("请选择垃圾的类别哟~~");
                return;
            }
            if(this.form.name == null || this.form.name == ""){
                this.$message.warning("请选择垃圾的名字哟~~");
                return;
            }

            let url = "/rubbish/insert";
            let message = "添加成功啦！！！";
            let bool = false;
            let param = {
                sortId: this.form.type,
                name: this.form.name
            };

            if(this.form.id != null && this.form.id != ""){
                param.id = this.form.id;
                url = "/rubbish/update";
                message = `修改第 ${this.idx + 1} 行成功`;
                bool = true;
            }
            this.$axios.get(this.GLOBAL.httpUrl + url, {
                params: param
            }).then(res=>{
                if(res.data.status==200) {
                    this.editVisible = false;
                    this.$message.success(message)
                    this.getData()
                    if(bool){
                        this.$set(this.tableData, this.idx, this.form);
                    }
                }else {
                    this.getData();
                    this.editVisible = false;
                    this.$message.info("修改失败");
                }
            }).catch(error=>{
                this.getData();
                this.editVisible = false;
                this.$message.info("修改异常");
            })
        },
        cancelEdit(){
            this.getData();
            this.editVisible = false;
            this.$message.warning("取消");
        },
        // 分页导航
        handleSizeChange(val) {
            this.$set(this.query, 'pageSize', val);
            this.getData();
        },
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        sortBtu(){
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        sortChange(val){
            this.$set(this.query, 'pageIndex', 1);
            this.$set(this.query, 'sortId', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
